package com.example.lenovo.appstore.view;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.appstore.R;
import com.example.lenovo.appstore.utils.AppConfig;
import com.example.lenovo.appstore.utils.AppContext;
import com.example.lenovo.interfaca.DialogInterfaca;

/**
 * 项目名称：AppStore
 *
 * @Author 郭迎辉
 * Create by 9:58 on 2019/3/22.
 * Describe:
 */
public class MyDialog implements View.OnClickListener {
    ViewHolder holder;
    Dialog bottomDialog;
    MyCount count;
    private boolean isFinish=false;
    DialogInterfaca interfaca;
  /*  DialogInterfaca dialogInterfaca;


    public void setDialogInterfaca(DialogInterfaca dialogInterfaca) {
        this.dialogInterfaca = dialogInterfaca;
    }

    static interface  DialogInterfaca{
        void finish(boolean finish);
    }*/


    public  void showDialog(Context mContext, final DialogInterfaca interfaca){
        bottomDialog = new Dialog(mContext, R.style.BottomDialog);
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.evaluation_historical_dialog, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();

//        bottomDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialogInterface) {
//                interfaca.finish(true);
//            }
//        });

        layoutParams.width = AppContext.getInstance().getResources().getDisplayMetrics().widthPixels;
        layoutParams.height = AppContext.getInstance().getResources().getDisplayMetrics().heightPixels;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.show();
        holder=new ViewHolder();
        holder.splashExitButton=(RelativeLayout) contentView.findViewById(R.id.splashExitButton);
        holder.splishTime=(TextView) contentView.findViewById(R.id.splish_time);
        holder.splash_imgView=(ImageView) contentView.findViewById(R.id.splash_imgView);
        holder.splash_imgView.setOnClickListener(this);
        //设置点击事件
       holder. splashExitButton.setOnClickListener(this);
        // 设置计时器显示
//        holder. splashExitButton.setVisibility(View.VISIBLE);
//        count = new MyCount(15 * 1000, 1000);
//        // 启动定时器
//        count.start();
            CountDownTimer timer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                holder.splishTime.setEnabled(false);
                holder.splishTime.setText( millisUntilFinished / 1000 +"");

            }

            @Override
            public void onFinish() {
                bottomDialog.dismiss();
                interfaca.finish(true);
            }
        }.start();

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.splashExitButton:
            case R.id.splish_time:
                break;
            case R.id.splash_imgView:
                bottomDialog.dismiss();
//                interfaca=new DialogInterfaca() {
//                    @Override
//                    public void finish(boolean isFinished) {
//                        isFinished=false;
//                    }
//                };
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://item.taobao.com/item.htm?spm=a217h.9580640.831227.7.71a525aaJEGeFM&id=587796754519&scm=1007.12144.81309.4362488_0_0&pvid=08a6992f-9377-48ba-81ee-d47efb21de74&utparam=%7B%22x_hestia_source%22%3A%224362488%22%2C%22x_object_type%22%3A%22item%22%2C%22x_mt%22%3A8%2C%22x_src%22%3A%224362488%22%2C%22x_pos%22%3A1%2C%22x_pvid%22%3A%2208a6992f-9377-48ba-81ee-d47efb21de74%22%2C%22x_object_id%22%3A587796754519%7D"));
                // 注意此处的判断intent.resolveActivity()可以返回显示该Intent的Activity对应的组件名
                // 官方解释 : Name of the component implementing an activity that can display the intent
                if (intent.resolveActivity(AppContext.getInstance().getPackageManager()) != null) {
                    final ComponentName componentName = intent.resolveActivity(AppContext.getInstance().getPackageManager());
                    // 打印Log   ComponentName到底是什么
                    Log.d("componentName = " , componentName.getClassName());
                    AppContext.getInstance().startActivity(Intent.createChooser(intent, "请选择浏览器"));
                } else {
                    Toast.makeText(AppContext.getInstance().getApplicationContext(), "请下载浏览器", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }


    static class ViewHolder {
        private RelativeLayout splashExitButton;
        TextView splishTime;
        ImageView splash_imgView;
    }
    //倒计时显示的类
    class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            Log.e("广告===","事件结束，关闭广告页面");
            this.cancel();
            bottomDialog.dismiss();


        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (null !=holder. splishTime) {
                holder.splishTime.setText((millisUntilFinished / 1000) + "");
            }
        }
    }
}
